import os
import shutil
import json
import xml.etree.ElementTree  as ET
from Utils import *
file_path = os.path.abspath(__file__)
dir_path = os.path.dirname(file_path).replace("\\file","")
srcAssets = dir_path +'\\mipmap-xhdpi'
desAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\res\\mipmap-xhdpi'
print('替换icon路径::\n'+desAssets+'\n')

copyFiles(srcAssets, desAssets)
resAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\res'
if(os.path.exists(resAssets + '\\mipmap-hdpi')):
    deleteDir(resAssets + '\\mipmap-hdpi')
if(os.path.exists(resAssets + '\\mipmap-mdpi')):
    deleteDir(resAssets + '\\mipmap-mdpi')
if(os.path.exists(resAssets + '\\mipmap-xxhdpi')):
    deleteDir(resAssets + '\\mipmap-xxhdpi')
shutil.rmtree(dir_path+'\\file\\__pycache__')



srcAssets = dir_path +'\\file\\AppActivity.java'
desAssets = dir_path.replace("tools","") +"build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\app\\src\\org\\cocos2dx\\javascript\\AppActivity.java"



##修改游戏广播文件
with open(srcAssets, "r", encoding="utf-8") as file:
    tex_read = file.read()
with open(dir_path.replace("\\tools","\\assets\\resources\\tvBoxDatas\\androidBroadcast.json"), 'r') as file:
    jsonBroadcastData = json.load(file)
if(jsonBroadcastData['isBroadcast'] == False):
    tex_read = tex_read.replace("if(true)//true:开启广播,false:关闭广播","if(false)//true:开启广播,false:关闭广播")
with open(desAssets, "w") as file:
    file.write(tex_read)
with open(dir_path.replace("\\tools","\\settings\\builder.json"), 'r') as file:
    jsonData = json.load(file)
tex_read = tex_read.replace("import org.cocos2d.demo","import "+jsonData['android']['packageName'])
with open(desAssets, "w") as file:
    file.write(tex_read)
print('AppActivity路径::\n'+desAssets+'\n')


#替换游戏名称
desAssets = dir_path.replace("\\tools","\\settings\\builder.json")
with open(desAssets, 'r') as file:
    jsonData = json.load(file)
srcAssets = dir_path +'\\file\\values.json'
with open(srcAssets, 'r', encoding='utf-8') as files:
    values = json.load(files)
if jsonData['android']['packageName'].find("jufeng") > 0 :
    appName = values["jufeng"]
elif jsonData['android']['packageName'].find("chensheng") > 0 :
    appName = values["chensheng"]
versionName = '"'+ values["versionName"]+'"'
srcAssets = dir_path.replace("\\tools","\\build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\res\\values\\strings.xml")
print("::::",srcAssets)
tree = ET.parse(srcAssets)
root = tree.getroot()
for node in list(root):
    node.text = appName
tree.write(srcAssets)

#替换数字键盘文件
srcAssets = dir_path +'\\file\\JniImp.cpp'
desAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\cocos2d-x\\cocos\\platform\\android\\jni\\JniImp.cpp'
with open(srcAssets, "r", encoding="utf-8") as file:
    tex_read = file.read()
with open(desAssets, "w", encoding="utf-8") as file:
    file.write(tex_read)
print('JniImp路径::\n'+desAssets+'\n')

srcAssets = dir_path +'\\file\\Cocos2dxGLSurfaceView.java'
desAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\cocos2d-x\\cocos\\platform\\android\\java\\src\\org\\cocos2dx\\lib\\Cocos2dxGLSurfaceView.java'
with open(srcAssets, "r", encoding="utf-8") as file:
    tex_read = file.read()
with open(desAssets, "w", encoding="utf-8") as file:
    file.write(tex_read)
print('Cocos2dxGLSurfaceView路径::\n'+desAssets+'\n')



srcAssets = dir_path +'\\file\\Cocos2dxActivity.java'
desAssets = dir_path.replace("tools","") +"build\\jsb-default\\frameworks\\cocos2d-x\\cocos\\platform\\android\\java\\src\\org\\cocos2dx\\lib\\Cocos2dxActivity.java"
with open(srcAssets, "r", encoding="utf-8") as file:
    tex_read = file.read()
with open(desAssets, "w", encoding="utf-8") as file:
    file.write(tex_read)
print('Cocos2dxActivity路径::\n'+desAssets+'\n')


desAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\app\\build.gradle'
print('版本号路径::\n'+desAssets+'\n')
with open(desAssets, "r", encoding="utf-8") as file:
    tex_read = file.read()
tex_write = ""
for temp_str in tex_read.split('\n'):
    if temp_str.find("versionName") > 0 :
        tex_write = tex_write+ temp_str.split('"')[0] + versionName+'\n';
    else :
        tex_write = tex_write+temp_str+"\n"
with open(desAssets, "w", encoding="utf-8") as file:
    file.write(tex_write)
desAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\instantapp\\build.gradle'
print('AndroidStudio版本号路径::\n'+desAssets+'\n')


with open(desAssets, "r", encoding="utf-8") as file:
    tex_read = file.read()
tex_write = ""
for temp_str in tex_read.split('\n'):
    if temp_str.find("versionName") > 0 :
        tex_write = tex_write+ temp_str.split('"')[0] + versionName+'\n';
    else :
        tex_write = tex_write+temp_str+"\n"
with open(desAssets, "w", encoding="utf-8") as file:
    file.write(tex_write)


print("广播状态::",jsonBroadcastData['isBroadcast'])
print("立刻退出状态::",jsonBroadcastData['isOnceExitGame'])
print("默认确实键状态::",jsonBroadcastData['isConstEnter'])
print("公司包名::",jsonData['android']['packageName'])
print("游戏名称::",appName)
print("游戏版本号::",versionName)

print('\n完成任务\n')

# E:\Siki\Cocos\tvBox_cc\build\jsb-default\frameworks\runtime-src\proj.android-studio\res
# E:\Siki\Cocos\tvBox_cc\build\jsb-default\frameworks\runtime-src\proj.android-studio\app\src\org\cocos2dx\javascript
# E:\Siki\Cocos\tvBox_cc\build\jsb-default\frameworks\cocos2d-x\cocos\platform\android\jni
# E:\Siki\Cocos\tvBox_cc\build\jsb-default\frameworks\cocos2d-x\cocos\platform\android\java\src\org\cocos2dx\lib